﻿namespace Busconeitor.Web.Controllers
{
    using System.Collections.Generic;
    using System.Web.Mvc;
    using System.Linq;
    using Models;
    using BusinessLayer;
    using Utils;

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Busconeitor";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Search(string query, int? page)
        {
            if (!string.IsNullOrEmpty(query))
            {
                var model = ObtenerTodosSinPaginar(query);
                return View(model);
            }

            return RedirectToAction("Index", "Home");
        }

        [HttpGet]
        public ActionResult GetPage(string query, int? page)
        {
            var model = new QueryModel { Query = query };
            model.Results = Paginacion(page);

            return PartialView("_Results", model);
        }

        [HttpGet]
        public FileResult Descarga(int id)
        {
            if (id > 0)
            {
                var rule = new DocumentRule();
                var document = rule.GetDocument(id);

                if (document != null)
                {
                    byte[] fileBytes = System.IO.File.ReadAllBytes(Params.DocumentPath + document.Location);

                    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, document.Name + ".pdf");
                }                
            }

            return null;
        }

        #region Private Methods

        private List<ResultModel> ObtenerTodos()
        {
            var list = new List<ResultModel>();

            for (var i = 1; i <= 50; i++)
            {
                var item = new ResultModel { Fragment = "Resultado " + i };
                list.Add(item);
            }

            return list;
        }

        private List<ResultModel> Paginacion(int? page)
        {
            const int pageSize = 10;
            var pagina = ObtenerTodos().Skip((page ?? 0) * pageSize).Take(pageSize).ToList();

            return pagina;
        }

        private QueryModel ObtenerTodosSinPaginar(string query)
        {
            var search = new SearchRule();
            var result = search.ProcessQuery(query);
            var queryModel = new QueryModel();
            queryModel.Query = query;

            if (result.TotalResults > 0)
            {                
                queryModel.TotalResults = result.TotalResults;
                queryModel.SearchTime = result.GetSearchTime();
                queryModel.Results = new List<ResultModel>();

                foreach (var item in result.Documents)
                {
                    var resultModel = new ResultModel();

                    resultModel.DocumentName = item.Name;
                    resultModel.Relevance = ((float)item.Relevance).ToString();
                    resultModel.PagesCount = item.PagesCount.ToString();
                    resultModel.Size = (item.Size / 1024) + " KB";
                    resultModel.Link = GenerateLink(item.Id);
                    resultModel.Fragment = item.Fragment;

                    queryModel.Results.Add(resultModel);
                }                
            }
            
            return queryModel;
        }

        private QueryModel ObtenerTodosSinPaginar2(string query)
        {
            var queryModel = new QueryModel();
            queryModel.Query = query;
            queryModel.TotalResults = 123;
            queryModel.SearchTime = "4.5";
            queryModel.Results = new List<ResultModel>();

            for (var i = 1; i < 20; i++)
            {
                var resultModel = new ResultModel();

                resultModel.DocumentName = "Name_" + i;
                resultModel.Relevance = "Relevance_" + i;
                resultModel.PagesCount = "PagesCount_" + i;
                resultModel.Size = "Size_" + i;
                resultModel.Link = GenerateLink(i);
                resultModel.Fragment = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus augue metus, auctor vitae condimentum non, porttitor quis augue...";

                queryModel.Results.Add(resultModel);
            }

            return queryModel;
        }

        private string GenerateLink(int id)
        {
            var url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
            var newUrl = url.Replace("Search", "Descarga");
            
            return newUrl + "?id=" + id;
        }

        #endregion
    }
}
